package com.rui1024.servlet;

import com.rui1024.dao.OrderDao;
import com.rui1024.dao.impl.OrderDaoImpl;
import com.rui1024.pojo.Order;
import com.rui1024.pojo.User;
import com.rui1024.util.JDBCUtil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@WebServlet("/OrderStatesServlet")
public class OrderStatesServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		OrderDao od = new OrderDaoImpl();
		JDBCUtil jdbcUtil = new JDBCUtil();
		Connection con=null;

		int id = 0;
		System.out.println("request.getParameter(\"id\") " + request.getParameter("id"));
//		System.out.println(id = Integer.parseInt(request.getParameter("id")));





		try {
			con = jdbcUtil.getCon();
			List<Order> list = od.getAllOrder(con);
			List<Integer> listadd = new ArrayList<>();
			int listTotalSum= od.ordersSumTotal(con);

			if(request.getParameter("id") != null){
				id = Integer.parseInt(request.getParameter("id"));
				od.deleteorder(id,con);
			}


			for (int i = 0; i <list.size() ; i++) {
				Order orderlist = (Order)list.get(i);
				listadd.add(orderlist.getAmount());
			}
			int num=0;
			for (int i = 0; i <listadd.size() ; i++) {
				num+=listadd.get(i);
			}
//			System.out.println(num);
//			System.out.println(listTotalSum);

			request.setAttribute("listTotalSum", listTotalSum);// 把数据存起来
			request.setAttribute("ordernum", num);// 把数据存起来
			request.setAttribute("orders", list);// 把数据存起来
			request.getRequestDispatcher("orders.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				jdbcUtil.closeCon(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
